package com.visionet.school.app.web.web.dynamic;

import com.visionet.school.app.web.entity.dynamic.DynamicComment;
import com.visionet.school.app.web.entity.dynamic.vo.DynamicCommentVo;
import com.visionet.school.app.web.service.dynamic.DynamicCommentService;
import com.visionet.school.app.web.service.dynamic.DynamicContentService;
import com.visionet.school.app.web.service.dynamic.DynamicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/1.
 */

@Controller
@RequestMapping("/dynamicComment")
public class DynamicCommentController {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private DynamicCommentService dynamicCommentService;

    @Autowired
    private DynamicContentService dynamicContentService;


    @RequestMapping(value = "/findOneCommentInfo", method = RequestMethod.GET)
    public ResponseEntity<?> findOneCommentInfo(String id){
        if(StringUtils.isBlank(id)){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("stat","success");
            map.put("dynamicComment", dynamicCommentService.findOne(id));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    /**
     * 提交评论
     * @param dynamicCommentVo
     * @return
     */
    @RequestMapping(value = "/commitComment",method = RequestMethod.POST)
    public ResponseEntity<?> commitComment(@RequestBody DynamicCommentVo dynamicCommentVo){
        if(dynamicCommentVo == null || StringUtils.isBlank(dynamicCommentVo.getComment()) || StringUtils.isBlank(dynamicCommentVo.getDynamicId())){
            return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
        }
        HashMap<String,Object> map = new HashMap<>();
        try {
            dynamicCommentService.save(dynamicCommentVo);
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    /**
     * 分页加载评论
     * @param dynamicCommentVo
     * @return
     */
    @RequestMapping(value = "/loadComment", method = RequestMethod.POST)
    public ResponseEntity<?> getEntrustPage(@RequestBody DynamicCommentVo dynamicCommentVo){
        if(dynamicCommentVo == null || StringUtils.isBlank(dynamicCommentVo.getDynamicId())){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("pageResult",dynamicCommentService.loadComment(dynamicCommentVo));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    /**
     * 获取楼中楼详情
     * @param dynamicCommentVo
     * @return
     */
    @RequestMapping(value = "/getCommentInfoPage", method = RequestMethod.POST)
    public ResponseEntity<?> getCommentPage(@RequestBody DynamicCommentVo dynamicCommentVo){
        if(dynamicCommentVo == null || StringUtils.isBlank(dynamicCommentVo.getId())){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("pageResult",dynamicCommentService.getCommentInfoPage(dynamicCommentVo,"createDate"));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    /**
     * 动态中删除评论
     * @param id
     */
    @RequestMapping(value = "/deletedComment", method = RequestMethod.GET)
    public ResponseEntity<?>  deletedComment(String id){
        if(StringUtils.isBlank(id)){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        Map<String,String> map = new HashMap<>();
        try {
            dynamicCommentService.deletedComment(id);
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    /**
     * 动态中点赞评论
     * @param id
     */
    @RequestMapping(value = "/likeComment", method = RequestMethod.GET)
    public ResponseEntity<?>  likeComment(String id){
        if(StringUtils.isBlank(id)){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        Map<String,String> map = new HashMap<>();
        try {
            dynamicCommentService.likeComment(id);
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }
}
